/* ==================================================================   
 * Created [2009-4-27 下午11:32:55] by Jon.King 
 * ==================================================================  
 * TSS 
 * ================================================================== 
 * mailTo:jinpujun@hotmail.com
 * Copyright (c) Jon.King, 2009-2012 
 * ================================================================== 
 */

package com.jinhe.tss.component.dynproperty.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;

import org.dom4j.Element;

import com.jinhe.tss.component.dynproperty.DynConstants;
import com.jinhe.tss.core.persistence.IEntity;
import com.jinhe.tss.core.util.XMLDocUtil;
import com.jinhe.tss.core.web.dispaly.tree.ITreeNode;
import com.jinhe.tss.core.web.dispaly.tree.TreeAttributesMap;
import com.jinhe.tss.core.web.dispaly.xform.IXForm;

/** 
 * <p> XFormTemplateTab.java </p> 
 * 
 * 唯一索引：xformTemplateId, code
 */
@Entity
@Table(name = "component_dyn_tab", uniqueConstraints = { 
        @UniqueConstraint(name="MULTI_CODE_XFORM_TAB_TEMPLATE", columnNames = { "xformTemplateId", "code" }),
        @UniqueConstraint(name="MULTI_NAME_XFORM_TAB_TEMPLATE", columnNames = { "xformTemplateId", "name" }) 
    })
@SequenceGenerator(name = "dyn_xtab_sequence", sequenceName = "dyn_xtab_sequence", initialValue = 1000, allocationSize = 10)
public class XFormTemplateTab implements IEntity, IXForm, ITreeNode{    
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "dyn_xtab_sequence")
    private Long    id;     //主键
    private String  name;   //表单页名称 
    private String  code;   //表单页code, 当只有当前动态属性实体存在多个表单时有用 
    private Integer seqNo;  //排序号 
    
    @Column(length = 4000)  
    private String  layout;  //表单布局，生成的xform 模板<layout/>节点
   
    private Long    xformTemplateId; // tab页所属的XForm表单ID 
    
    @Transient
    private String  declare; //表单声明，生成的xform 模板<declare/>节点 
    
    @Transient
    private List<Long> propertyIds = new ArrayList<Long>();
    public  List<Long> getPropertyIds(){ 
        return this.propertyIds; 
    }
 
    public String getDeclare() {
        if(this.layout == null) return null;
        
        Element declareNode = (Element) XMLDocUtil.dataXml2Doc(this.layout).selectSingleNode("//xform/declare");
        this.declare = declareNode.asXML();
        return this.declare;
    }
    
    public String getDeclareOnly() {
        return this.declare;
    }
  
    public String getLayoutOnly() {
        if (this.layout == null)
            return null;
        Element layoutNode = (Element) XMLDocUtil.dataXml2Doc(this.layout).selectSingleNode("//xform/layout");
        StringBuffer sb = new StringBuffer();
        for (Iterator<?> it = layoutNode.elementIterator(); it.hasNext();) {
            Element node = (Element) it.next();
            sb.append(node.asXML());
        }
        return sb.toString();
    }

    public TreeAttributesMap getAttributes() {
        TreeAttributesMap map = new TreeAttributesMap(id, name);
        map.put("code", this.code);
        map.put("xformTemplateId", this.xformTemplateId);
        map.put("type", "" + DynConstants.TAB_TYPE);
        return map;
    }
    
    public Map<String, Object> getAttributesForXForm() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", this.id);
        map.put("name", this.name);
        map.put("code", this.code);
        map.put("seqNo", this.seqNo);
        map.put("layout", this.getLayout());
        map.put("declare", this.getDeclare());
        map.put("xformTemplateId", this.xformTemplateId);
        return map;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getLayout() {
        return layout;
    }

    public void setLayout(String layout) {
        this.layout = layout;
    }

    public Integer getSeqNo() {
        return seqNo;
    }

    public void setSeqNo(Integer seqNo) {
        this.seqNo = seqNo;
    }

    public Long getXformTemplateId() {
        return xformTemplateId;
    }

    public void setXformTemplateId(Long xformTemplateId) {
        this.xformTemplateId = xformTemplateId;
    }

    public void setPropertyIds(List<Long> propertyIds) {
        this.propertyIds = propertyIds;
    }
    
}

